package service.impl;

import context.LoginUserCache;
import controller.entity.Page;
import dao.GoodsDAO;
import entity.Goods;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.GoodsService;

import java.util.List;

/**
 * Created by asus on 2017/6/19.
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDAO goodsDAO;

    /**
     * 分页查询商品信息
     *
     * @return
     */
    @Override
    public List<Goods> getGoods(Page page) {
        List<Goods> goodsList = null;
        System.out.println("getGoods中测试LoginUserCache是否起作用：");
        User user= LoginUserCache.get("root");
        System.out.println(user);
        goodsList = (List<Goods>) goodsDAO.findPage(page.getPage(), page.getRows());
        if (null == goodsList || goodsList.size() < 1) {
            System.out.println("GoodsService--->getGoods：商品为空");
        }
        return goodsList;
    }

    /**
     * 获取总记录数
     *
     * @return
     */
    @Override
    public Integer getCount() {
        return goodsDAO.getCount();
    }

    @Override
    public Long saveGoods(Goods goods) {
        return goodsDAO.save(goods);
    }

    @Override
    public List<Goods> getAllGoods() {
        return goodsDAO.findAll();
    }

    @Override
    public void update(Goods goods) throws Exception {
        try{
            goodsDAO.update(goods);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("修改商品失败");
        }
    }

    @Override
    public void delete(Long id) throws Exception {
        goodsDAO.delete(id);
    }
}
